from zope.interface import Interface, implements
from zope import schema
from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
from plone.i18n.locales.countries import countries

from apkn.extensions import _
from plone.app.users.userdataschema import IUserDataSchemaProvider
from plone.app.users.userdataschema import IUserDataSchema


GENDERS = (('female','female',_('Female')),
           ('male', 'male', _('Male')),
           )

@apply
def country_vocabulary():
    terms = []
    country_listing = countries.getCountryListing()
    s_country_listing = sorted(country_listing, key=lambda c:c[0])
    for value,title in s_country_listing:
        terms.append(SimpleTerm(value=value, token=value, title=title))
    return SimpleVocabulary(terms)

@apply
def gender_vocabulary():
    terms = []
    for value, token, title in GENDERS:
        terms.append(SimpleTerm(value=value, token=token, title=title))
    return SimpleVocabulary(terms)

class UserDataSchemaProvider(object):
    implements(IUserDataSchemaProvider)

    def getSchema(self):
        """
        """
        return IEnhancedUserDataSchema

class IEnhancedUserDataSchema(IUserDataSchema):
    """ Use all the fields from the default user data schema, and add various
    extra fields.
    """
    firstname = schema.TextLine(
        title=_(u'label_firstname', default=u'First name'),
        description=_(u'help_firstname',
                      default=u"Fill in your given name."),
        required=False,
        )
    lastname = schema.TextLine(
        title=_(u'label_lastname', default=u'Last name'),
        description=_(u'help_lastname',
                      default=u"Fill in your surname or your family name."),
        required=False,
        )
    gender = schema.Choice(
        title=_(u'label_gender', default=u'Gender'),
        description=_(u'help_gender',
                      default=u"Tell us your gender?"),
        source = gender_vocabulary,
        required=False,
        )
    birthdate = schema.Date(
        title=_(u'label_birthdate', default=u'birthdate'),
        description=_(u'help_birthdate', 
            default=u'Your date of birth, in the format dd-mm-yyyy'),
        required=False,
        )
    city = schema.TextLine(
        title=_(u'label_city', default=u'City'),
        description=_(u'help_city',
                      default=u"Fill in the city you live in."),
        required=False,
        )
    country = schema.Choice(
        title=_(u'label_country', default=u'Country'),
        description=_(u'help_country',
                      default=u"Fill in the country you live in."),
        source=country_vocabulary,
        required=False,
        )
    phone = schema.TextLine(
        title=_(u'label_phone', default=u'Telephone number'),
        description=_(u'help_phone',
                      default=u"Leave your phone number so we can reach you."),
        required=False,
        )
